package com.jwthhealth_pub.old.report.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.base.BaseActivity;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.report.model.resbody.HraDataRes;
import com.jwthhealth_pub.old.report.view.adapter.ReportEnvironmentAdapter;
import com.jwthhealth_pub.old.report.view.widget.JwthBarChart;
import com.jwthhealth_pub.old.report.view.widget.Jwth_CircleProgressbar;
import com.jwthhealth_pub.old.report.view.widget.linechart.JwthLineChart;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ReportEnvironmentActivity extends BaseActivity {

    @BindView(R.id.title_desc)
    TextView titleDesc;
    @BindView(R.id.jwth_barchart)
    JwthBarChart jwthBarchart;
    private int mIndex;
    private String[] mLabels;
    private float[] mValues;
    private boolean isCardInit;
    private HraDataRes.DataBean.NhjBean.SonBeanXXX mData;
    private JwthLineChart mLineChart;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reprot_physical_detail);
        ButterKnife.bind(this);

        Intent intent = getIntent();
        mIndex = intent.getIntExtra("index", 0);
        mData = intent.getParcelableExtra("data");

        isCardInit = false;

//        mCardView = findViewById(R.id.chart_card);
        mLineChart = findViewById(R.id.linechart);

//        SecCircleProgressbar mCircle =  findViewById(R.id.circle);
        Jwth_CircleProgressbar mCircle = findViewById(R.id.circle);


        // circle title
        TextView title = findViewById(R.id.tv_title);

        title.setText(mData.getHra_type() + "当前得分");
        Log.d(Constant.DEBUGTAG, mData.getScore());
        //        mCircle.setProgressbar((int) (score/100*360));
        float _score = Float.parseFloat(mData.getScore());
        mCircle.setProgressbar((int) (_score / 100 * 220), _score);

        if (mData != null)
            intDescribe(mIndex, mData);

        if (mIndex == 1) {
//            mCardView.setVisibility(ViewHolder.GONE);
            mLineChart.setVisibility(View.VISIBLE);
            jwthBarchart.setVisibility(View.GONE);
        } else {
//            mCardView.setVisibility(ViewHolder.VISIBLE);
//            mCardLineView.setVisibility(ViewHolder.GONE);
//            mStackedCard = new StackedCardTwo(mCardView);
//            mCardView.setVisibility(ViewHolder.VISIBLE);
            mLineChart.setVisibility(View.GONE);
            jwthBarchart.setVisibility(View.VISIBLE);


            // lables 和 values 长度必须相同
            mLabels = new String[mData.getSon().size()];
            mValues = new float[mData.getSon().size()];

            jwthBarchart.setyAxisValues(mValues);
            switch (mIndex) {
                case 0:
                    jwthBarchart.setWarnLines(new int[]{2, 4});
                    break;
                case 2:
                    // TODO: 2017/10/18 一个就可以
                    jwthBarchart.setWarnLines(new int[]{2, 2});
                    break;
                case 3:
                    jwthBarchart.setWarnLines(new int[]{6, 10});
                    break;
                case 4:
                    jwthBarchart.setWarnLines(new int[]{2, 4});
                    break;
                case 5:
                    jwthBarchart.setWarnLines(new int[]{2, 4});
                    break;
                default:
                    break;
            }

            Log.d("ReportEnvironmentActivi", "mData.getSon():" + mData.getSon().size());
            for (int i = 0; i < mData.getSon().size(); i++) {
                HraDataRes.DataBean.NhjBean.SonBeanXXX.SonBeanXX sonBeanXX = mData.getSon().get(i);
                String value = sonBeanXX.getValue();
                if (sonBeanXX.getValue() == null) {
                    Log.d("ReportPhysicalDetailAct", "value is null");
                    value = "0";
                }
                if (value.equals("标准")) value = "0";

                Log.d(Constant.DEBUGTAG, value);

                Log.d("ReportPhysicalDetailAct", value);
                mValues[i] = Float.parseFloat(value);
                mLabels[i] = "";
            }
        }

    }

    @Override
    protected void title() {
        TitleLayout titleLayout = findViewById(R.id.title_layout);
        titleLayout.setTitle(mData.getHra_type());
        titleLayout.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                finish();
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
        if (mData.getTotal() != null && mData.getBelow() != null && mData.getPercent() != null) {
            titleDesc.setText("本系统测试项共" + mData.getTotal() + "项, 您超出范围的有" + mData.getBelow() + "项,占" + mData.getPercent());
        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

        if (!isCardInit) {
            isCardInit = true;

//            int height = mChartCardView.getHeight();
//            ViewGroup.LayoutParams layoutParams = mChartCardView.getLayoutParams();
            switch (mIndex) {
                // TODO: 2017/10/16 最大值最小值步长目前必须写死
                case 0:
                    jwthBarchart.setxAxisValues(-15f, 15f, 5f);
//                    jwthBarchart.setyAxisValues(mValues);
                    break;
                case 1:
                    // 折线图
//                    layoutParams.height = height / 7 * 5;
                    List<HraDataRes.DataBean.NhjBean.SonBeanXXX.SonBeanXX> son = mData.getSon();
                    float targetX = 0, targetY = 0;
                    for (int i = 0; i < son.size(); i++) {
                        HraDataRes.DataBean.NhjBean.SonBeanXXX.SonBeanXX sonBeanXX = son.get(i);
                        if (sonBeanXX.getC_name().equals("SBE"))
                            targetY = Float.parseFloat(sonBeanXX.getValue());
                        if (sonBeanXX.getC_name().equals("PaCO2"))
                            targetX = Float.parseFloat(sonBeanXX.getValue());
                    }
                    mLineChart.setTArgetLoa(targetX, targetY);
                    break;
                case 2:
                    jwthBarchart.setxAxisValues(0, 25f, 5f);
//                    layoutParams.height = height / 7 * 5;
                    break;
                case 3:
                    jwthBarchart.setxAxisValues(-40, 40f, 5f);
//                    layoutParams.height = height / 7 * 4;
                    break;
                case 4:
                    jwthBarchart.setxAxisValues(-15f, 15f, 5f);
//                    layoutParams.height = height / 7 * 5;
                    break;
                case 5:
                    jwthBarchart.setxAxisValues(-60f, 60f, 20f);
//                    layoutParams.height = height / 7 * 12;
                    break;
                default:
                    break;
            }

//            mChartCardView.setLayoutParams(layoutParams);


//            if (mStackedCard == null) return;
//            mStackedCard.setmIndex(mIndex);
//            mStackedCard.setmLabels(mLabels);
//            mStackedCard.setmValues(mValues);
//            mStackedCard.init();


        }
    }

    private void intDescribe(int index, HraDataRes.DataBean.NhjBean.SonBeanXXX mData) {
        RecyclerView rv = findViewById(R.id.rv_describe);

        CustomLinearLayoutManager linearLayoutManager = new CustomLinearLayoutManager(this);
//        linearLayoutManager.setScrollEnabled(false);
//        FullyLinearLayoutManager fullyLinearLayoutManager = new FullyLinearLayoutManager(this);
        rv.setLayoutManager(linearLayoutManager);

//        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(new ReportEnvironmentAdapter(this, index, mData));
    }


    private class CustomLinearLayoutManager extends LinearLayoutManager {
        private boolean isScrollEnabled = true;

        CustomLinearLayoutManager(Context context) {
            super(context);
        }

//        public void setScrollEnabled(boolean flag) {
//            this.isScrollEnabled = flag;
//        }

        @Override
        public boolean canScrollVertically() {
            //Similarly you can customize "canScrollHorizontally()" for managing horizontal scroll
            return isScrollEnabled && super.canScrollVertically();
        }
    }


}
